package com.produtos;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class NovoProduto extends Activity implements OnClickListener {
	
	Button btadicionar, btTirarFoto, btn_scan;
	EditText edtProduto, edtDesc, edtPreco, edtQtd;
	ImageView iv;
	String barCode;
	int aux = 0;

	final static int cameraData = 0;
	private static final int REQUEST_BARCODE = 0;
	
	private DBAdapter datasource;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
			setContentView(R.layout.novoproduto);
			
			Bundle extras = getIntent().getExtras();
			if(extras != null){
				barCode = extras.getString("barCode");
			}
			
			datasource = new DBAdapter(this);
			edtProduto = (EditText) findViewById(R.novoproduto.edtProduto);
			edtDesc = (EditText) findViewById(R.novoproduto.edtDesc);
			edtPreco = (EditText) findViewById(R.novoproduto.edtPreco);
			edtQtd = (EditText) findViewById(R.id.editText1);
			btadicionar =  (Button) findViewById(R.novoproduto.btadicionar);
			btTirarFoto =  (Button) findViewById(R.novoproduto.tirarFoto);
			btn_scan = (Button) findViewById(R.novoproduto.btn_scan);
			iv = (ImageView) findViewById(R.novoproduto.ivReturnedPic);
			
			btn_scan.setOnClickListener(this);
			btadicionar.setOnClickListener(this);
			btTirarFoto.setOnClickListener(this);
			
			if(aux == 0){
			btn_scan.performClick();
			aux++;
			}
	}
	
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(arg0 == btTirarFoto){
					Intent i= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
					startActivityForResult(i,cameraData);
				}
				if(arg0 == btadicionar){
					datasource.open();		
					Produto p = datasource.createProduto(barCode, edtProduto.getText().toString(), edtDesc.getText().toString(),edtPreco.getText().toString(),edtQtd.getText().toString(),loadBitmapFromView(iv));
					datasource.close();	
					AlertDialog.Builder dialogo = new
							AlertDialog.Builder(NovoProduto.this);
							dialogo.setTitle("Aviso");
							dialogo.setMessage("Produto: " + p.getProduto() + "\nCódigo de Barra: " + p.getCode());			
				dialogo.setNeutralButton("OK", new DialogInterface.OnClickListener() {  
								   public void onClick(DialogInterface dialog, int id) {  
								    finish();
							      }  
							});  
							dialogo.show(); 
					
				}
				if(arg0 == btn_scan){
					Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
					intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Diga o nome do produto.");
					startActivityForResult(intent, 1111);
				}
			}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	// TODO Auto-generated method stub
		if(resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			Bitmap bmp = (Bitmap) extras.get("data");
			iv.setImageBitmap(bmp);
		}
		if(requestCode == REQUEST_BARCODE){
			String barcode = data.getStringExtra("SCAN_RESULT");
		}
		
		if(requestCode == 1111){
			ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			String nom = results.get(0).toString();
			edtProduto.setText(nom);
			AlertDialog.Builder editalert = new AlertDialog.Builder(this);
			
			editalert.setTitle("Preço");
			editalert.setMessage("Qual o valor do produto?");


			final EditText input = new EditText(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
			        LinearLayout.LayoutParams.FILL_PARENT,
			        LinearLayout.LayoutParams.FILL_PARENT);
			input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
			input.setLayoutParams(lp);
			editalert.setView(input);

			editalert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int whichButton) {
			    	edtPreco.setText(input.getText().toString());

			    }
			});
			editalert.show();
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public static Bitmap loadBitmapFromView (View v) {
		Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
	    Canvas c = new Canvas(b);
	    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
	    v.draw(c);
	    return b;
}

}


